
/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
package tablization;

import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/*import autoInheritance.AutoInheritanceCreation;
import autoInheritance.CombinationSearching;*/
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class TestTablization
{

	DBManager sqlDB;
	GetIdName gin;	
	
	String tablization="tablization";
	String strPid="Type";
	String column="column";
	
	SearchingPropeties sp;
	/*CombinationSearching cs;
	AutoInheritanceCreation aic;*/
	
	public TestTablization(DBManager db) {
		
		this.sqlDB=db;
		gin= new GetIdName(sqlDB);
	}
	
	private void getType(String type,String NoOfProperties,String NoOfObjects) 
	{
		Vector<Object> objects=new Vector<Object>();
		Vector<Object> properties=new Vector<Object>();	
		
		Hashtable<Object,Object> hasht=new Hashtable<Object, Object>();
		
		try
		{
			/*sp=new SearchingPropeties();
			hasht=sp.searchProperty(type);		
		
			cs=new CombinationSearching();
			cs=cs.searchObjects(hasht,NoOfProperties,NoOfObjects);				
		
			for(int i=0;i<cs.objectVector.size();i++)
				objects=(Vector<Object>)cs.objectVector.get(i);
		
			for(int i=0;i<cs.propertyVector.size();i++)
				properties=(Vector<Object>)cs.propertyVector.get(i);*/
			
			String tableName=getMaxId();
			createTable(objects,properties,tableName);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void createTable(Vector<Object> objVector,Vector<Object> propVector,String tableName) 
	{
		ResultSet rs;
		for(int i=0;i<objVector.size();i++)
		{
			String strMid=objVector.get(i).toString();			
			String query="select mid,pid,pv,vt,ref from property_details where mid='"+strMid+"' order by pid";
			rs=sqlDB.getSelect(query);
			
			try
			{
					while(rs.next())
					{
						String pid=rs.getString("pid");
						for(int j=0;j<propVector.size();j++)
						{
							Hashtable<Object,Object> h=(Hashtable<Object, Object>)propVector.get(j);
							String tempPid=h.get("pid").toString();									
							
							if(tempPid.equals(pid))
							{						
								String strPv=rs.getString("pv");					
								if(!strPv.equals("0"))
									strPv=gin.getItem(strPv);
								else
									strPv=rs.getString("vt");	
							}
						}
					}						
				
			}
			catch (Exception e)
			{
				
			}
			
		}
	}
	
	private String getMaxId()
	{
		ResultSet rs;
		String mid="";
		try
		{			
			rs = sqlDB.getSelect("select id from max_id_table where table_name = 'master_table'");
			rs.next();
			String id = rs.getString("id");
			rs.close();
			
			mid="table_"+id;			
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return mid;
	}
	
	@SuppressWarnings("unchecked")
	/*public void checkTable(String request)
	{
		Vector<Object> propVector=new Vector<Object>();
		Vector<Object> finalVector=new Vector<Object>();
		//Vector<Object> countVector=new Vector<Object>();
		//ObjectDetection od=new ObjectDetection(sqlDB);
		//propVector=od.getProperties(request);		
		//int columnCount=propVector.size();
		ResultSet rs;
		for(int i=0;i<propVector.size();i++)
		{
			Hashtable<Object,Object> hasht=(Hashtable<Object, Object>)propVector.get(i);
			String pv=hasht.get("pv").toString();
			String query="select mid from property_details where pid='"+pid+"' and pv='"+pv+"'";
			rs=sqlDB.getSelect(query);
			try
			{			
				while(rs.next())
				{
					String tableName=rs.getString(1);
					finalVector.add(tableName);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		for(int i=0;i<finalVector.size();i++)
		{
			String name1=finalVector.get(i).toString();
			for(int j=0;j<finalVector.size();j++)
			{
				String name2=finalVector.get(j).toString();
				if(i!=j)
				{
					if(name1.equals(name2))
					{
						counter++;
					}
				}
			}
			
			counter=0;
		}
		
	}*/
	
	/*Vector< Object>MidVector = new Vector<Object>();
	Hashtable<Object, Object> objectHash = new Hashtable<Object, Object>();

	RandomAccessFile midFile;
	String TypePV=" ";
	String strPid="Type";
	DBManager sqlDB;
	GetIdName  gin; 
	
	public SearchingPropeties(DBManager database) 
	{
		this.sqlDB=database;
		gin=new GetIdName(sqlDB);
		strPid=gin.getId(strPid);
	}
	public SearchingPropeties()
	{
		sqlDB=new DBManager();
		sqlDB.getConnect();
		gin=new GetIdName(sqlDB);
		strPid=gin.getId(strPid);
	}


	*//**
	 * 
	 * @param pv contains the property values for property pid=type.
	 * @param MidVector contains all object that is mid where pid=type.
	 * @param rootPropertyVector is a vector which cointains all properties
	 * of first object that is root object.   
	 * @param objPropertyVector is vectors that cointains all properties 
	 * of remaining objects one by one.
	 * @param objectHash is Hashtable in which particular property acts as a key 
	 * and all obects which contains that property acts as a value of a Hashtable.
	 * 
	 *  
	 *//*
	@SuppressWarnings("unchecked")
	public Hashtable<Object,Object> searchProperty(String pv)
	{
		
		Vector< Object>rootPropertyVector = new Vector<Object>();		
		ResultSet rs;
		String objMid="";
		TypePV = gin.getId(pv); 		
		String query="select distinct mid from property_details where pid='"+strPid+"' and pv='"+TypePV+"'";
		rs=sqlDB.getSelect(query);	
		try
		{
			while(rs.next())
			{
				 objMid=rs.getString(1);
				 if(!MidVector.contains(objMid))
					MidVector.add(objMid);
			}	
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}		
		
		System.out.println("size of mids "+MidVector.size());
		
		objMid=MidVector.get(0).toString();
		
		rootPropertyVector=getObjectProperties(objMid);
		Hashtable<Object, Object> objPropertyHash = new Hashtable<Object, Object>(); 
		
	    for(int k=0;k<rootPropertyVector.size();k++)
		{
	    	objPropertyHash=(Hashtable<Object, Object>) rootPropertyVector.get(k);
			System.out.println("rootPropertyHash**"+objPropertyHash);
			
			Vector<Object>objVector=new Vector<Object>();
			objVector.add(objMid);
			objectHash.put(objPropertyHash, objVector);
		}
	    
	    objPropertyHash = new Hashtable<Object, Object>(); 
	    rootPropertyVector = new Vector<Object>();
	    objMid="";
	    
	    for(int j=1;j<MidVector.size();j++)
		{
			objMid=MidVector.get(j).toString();
			rootPropertyVector=getObjectProperties(objMid);
					
			for(int y=0;y<rootPropertyVector.size();y++)
			{
				objPropertyHash = (Hashtable<Object, Object>) rootPropertyVector.get(y);
				System.out.println("objPropertyHash"+objPropertyHash);

				if(objectHash.containsKey(objPropertyHash))
				{
					Vector<Object>objVector=new Vector<Object>();
					objVector.addAll((Vector<Object>) objectHash.get(objPropertyHash));
					if(!objVector.contains(objMid))
							objVector.add(objMid);
					objectHash.put(objPropertyHash, objVector);
				}
				else
				{
					Vector<Object>objVector=new Vector<Object>();
					objVector.add(objMid);
					objectHash.put(objPropertyHash, objVector);
				}				
    		 }		
		}  		
			System.out.println("  property searching finished "+objectHash.size());
			
	   return objectHash;
	}
	
	*//**
	 * 
	 * @param mid is a object or Mid of property where pid=type.  
	 * @return propertyVector  which cointans all properties of object
	 * which is passed to this function.
	 *  
	 *//*
	public Vector<Object> getObjectProperties(Object mid)
	{
		ResultSet rs;
		Vector< Object>propertyVector = new Vector<Object>();
	
		String query="select pid from property_details where mid='"+mid+"'";
		rs=sqlDB.getSelect(query);	
		
		try
		{
			while(rs.next())
			{
				Hashtable<Object, Object> hashProperty = new Hashtable<Object, Object>(); 
				String clmStr="pid";
				String value=rs.getString(1);				
				hashProperty.put(clmStr, value);
				propertyVector.add(hashProperty);	
			}			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return propertyVector;
	}	*/
	
	public static void main(String[] args)
	{
		DBManager db=new DBManager();
		db.getConnect();
		TestTablization tt=new TestTablization(db);
		tt.getType("testType","3","2");
	}
	
	{/*

		DBManager sqlDB;
		GetIdName gin;	
		
		String tablization="tablization";
		String strPid="Type";
		String column="column";
		SearchingPropeties sp;
		CombinationSearching cs;
		AutoInheritanceCreation aic;	
		
		Hashtable<Object,Object> duplicateHashtable= new Hashtable<Object, Object>();
		
		public Test(DBManager sqlDB) 
		{
			this.sqlDB=sqlDB;
			gin=new GetIdName(sqlDB);
			strPid=gin.getId(strPid);
			column=gin.getId(column);
			//aic=new AutoInheritanceCreation(sqlDB);
		}
		
		
		private String getMaxId()
		{
			ResultSet rs;
			String mid="";
			try
			{			
				rs = sqlDB.getSelect("select id from max_id_table where table_name = 'master_table'");
				rs.next();
				String id = rs.getString("id");
				rs.close();
				
				mid=id;			
				
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			return mid;
		}
		

		
		private void insertInMaster(String tableName,Vector<Object> columnVector)
		{
			String request="11 master_table add*"+tableName+"|Type:table |Storage:database";
			
			for(int j=0;j<columnVector.size();j++)
				request=request+"|Column:"+columnVector.get(j).toString();	
			
			request=request+"#605";
			
			System.out.println("Column list is "+request);
			
			//aic.callControlClass(request);		
		}
		
		public void searchType(String type,String NoOfProperties,String NoOfObjects)
		{
			
			ResultSet rs;
			String delRequest="",subColumnTable="",request="",table="";		
			Vector<Object> objects=new Vector<Object>();
			Vector<Object> properties=new Vector<Object>();	
			Vector<Object> columnVector=new Vector<Object>();
			Vector<Object> duplicateVector;
			Hashtable<Object,Object> hasht=new Hashtable<Object, Object>();
			try
			{
				String pv=gin.getId(type);
				rs=sqlDB.getSelect("select count(*) from property_details where pid='"+strPid+"' and pv='"+pv+"'");
				rs.next();
				int obj=Integer.parseInt(rs.getString(1));
				
				if(obj>Integer.parseInt(NoOfObjects))
				{
						sp=new SearchingPropeties();
						hasht=sp.searchProperty(type);		
					
						cs=new CombinationSearching();
						cs=cs.searchObjects(hasht,NoOfProperties,NoOfObjects);				
					
						for(int i=0;i<cs.objectVector.size();i++)
							objects=(Vector<Object>)cs.objectVector.get(i);
					
						for(int i=0;i<cs.propertyVector.size();i++)
							properties=(Vector<Object>)cs.propertyVector.get(i);
						
						columnVector.add(0,"id");					
						//String tableName=getMaxId();
						
						String tableName=type+getMaxId();
						
						for(int i=0;i<properties.size();i++)
						{
							Hashtable<Object,Object> h=(Hashtable<Object, Object>)properties.get(i);
							String tempPid=h.get("pid").toString();			
							String column=gin.getItem(tempPid);				
							columnVector.add(i+1,column.toLowerCase());
						}	
						sqlDB.createTable(tableName, columnVector);
						
						insertInMaster(tableName, columnVector);				
						
						for(int i=0;i<objects.size();i++)
						{
							hasht=new Hashtable<Object, Object>();
							duplicateVector=new Vector<Object>();
							
							String strMid=objects.get(i).toString();			
							String object=gin.getItem(strMid);			
							delRequest="11 property_details del *"+object;	
							
							hasht.put(columnVector.get(0).toString(),strMid);
							
							String query="select mid,pid,pv,vt,ref from property_details where mid='"+strMid+"' order by pid";
							rs=sqlDB.getSelect(query);
							try
							{
								while(rs.next())
								{
									String pid=rs.getString("pid");
									for(int j=0;j<properties.size();j++)
									{
										Hashtable<Object,Object> h=(Hashtable<Object, Object>)properties.get(j);
										String tempPid=h.get("pid").toString();									
										
										if(tempPid.equals(pid))
										{						
											String strPv=rs.getString("pv");					
											if(!strPv.equals("0"))
												strPv=gin.getItem(strPv);
											else
												strPv=rs.getString("vt");								
											
											if(hasht.containsKey(tableName+"."+columnVector.get(j+1).toString()))
											{
												String temp=hasht.get(tableName+"."+columnVector.get(j+1).toString()).toString();
												Hashtable<Object,Object> ht=new Hashtable<Object, Object>();
												ht.put(tableName+"."+columnVector.get(j+1).toString(),temp);
												duplicateVector.add(ht);
											}						
												
											hasht.put(tableName+"."+columnVector.get(j+1).toString(), strPv);
											delRequest=delRequest+"|"+columnVector.get(j+1).toString()+":"+strPv;
											
											String strRef=rs.getString("ref");
											if(!strRef.equals("0"))
											{
												pid=gin.getItem(pid);
												String queryRef="alter table "+tableName+" add column "+pid+"_ref longtext";
												sqlDB.getExecute(queryRef);
												strRef=gin.getItem(strRef);												
												hasht.put(pid+"_ref",strRef);								
											}
											break;
										}					
									}					
								}
							}
							catch (Exception e) {
								// TODO: handle exception
							}	
							
							delRequest=delRequest+"#605";
							System.out.println("Delete Request "+delRequest);
							request="11 property_details add *"+object+"|Table :"+tableName+"#605";	
							
							if(!duplicateVector.isEmpty())
							{
								Vector<Object> tempVector=new Vector<Object>();
								for(int j=0;j<duplicateVector.size();j++)
								{
									Hashtable<Object,Object> temp1=(Hashtable<Object, Object>)duplicateVector.get(j);
									Enumeration<Object> enumt=temp1.keys();
									String str=enumt.nextElement().toString();
									if(hasht.containsKey(str))
									{
										String temp=hasht.get(str).toString();
										temp1=new Hashtable<Object, Object>();
										temp1.put(str,temp);
										duplicateVector.add(temp1);
										tempVector.add(temp1);	
										hasht.remove(str);
									}
									if(!duplicateHashtable.containsKey(str))
									{
										duplicateHashtable.putAll(temp1);
										
											str=str.replace('.','~');
											String []strArr=str.split("~");
											str=strArr[1];									
											Vector<Object> colVector=new Vector<Object>();
											colVector.add("id");
											colVector.add(str);
												
											subColumnTable=str+getMaxId();
											sqlDB.createTable(subColumnTable,colVector);
											insertInMaster(subColumnTable, colVector);
									}
								}
								Enumeration<Object> enumt=duplicateHashtable.keys();
								while(enumt.hasMoreElements())
								{
									String subTable=enumt.nextElement().toString();
									for(int k=0;k<tempVector.size();k++)
									{
										Hashtable<Object,Object> temp1=(Hashtable<Object, Object>)tempVector.get(k);
										if(temp1.containsKey(subTable))
										{
											String ref=subTable.replace('.','~');
											String []strArr=ref.split("~");														
											ref=strArr[1];	
											ref=ref+"_ref";
											
											if(hasht.containsKey(ref))
											{
												hasht.remove(ref);
												rs=sqlDB.getSelect("select "+ ref+" from "+tableName);
												try
												{	
													rs.next();
													String strRef=rs.getString(1);												
												}
												catch (Exception e)
												{
													String queryRef="alter table "+tableName+" drop column "+ref;
													sqlDB.getExecute(queryRef);											
												}											
											}
											
											hasht.put(subTable,subColumnTable);
											setInSubTable(strMid,subTable,subColumnTable);
										}
									}
								}				
								sqlDB.getInsert(tableName, hasht);							
							}
							else
								sqlDB.getInsert(tableName, hasht);						
							
							System.out.println("Request is "+request);
								//aic.callControlClass(request);	
							System.out.println("Request is "+delRequest);
								//aic.callControlClass(delRequest);
						}
						System.out.println("Table created successfully");
						}
						
					}
					catch (Exception e) 
					{
							// TODO: handle exception
					}					
		}
		
		
		public void setInSubTable(String mid,String subName,String subColumnTable)
		{
			
			ResultSet rs;
		
			Hashtable<Object,Object> hasht=new Hashtable<Object, Object>();
			
			subName=subName.replace('.','~');
			String []strArr=subName.split("~");	
			subName=strArr[1];
			String strPid=gin.getId(subName);	
			
			rs=sqlDB.getSelect("select * from property_details where mid='"+mid+"' and pid='"+strPid+"'");
			try
			{
				while(rs.next())
				{	
					hasht=new Hashtable<Object, Object>();
					hasht.put("id",mid);
					
					String strPv=rs.getString("pv");					
					if(!strPv.equals("0"))
						strPv=gin.getItem(strPv);
					else
						strPv=rs.getString("vt");	
					
					hasht.put(subColumnTable+"."+subName, strPv);
					String strRef=rs.getString("ref");
					if(!strRef.equals("0"))
					{
						String queryRef="alter table "+subColumnTable+" add column "+subName+"_ref longtext";
						sqlDB.getExecute(queryRef);
						strRef=gin.getItem(strRef);								
						hasht.put(subName+"_ref",strRef);								
					}
					sqlDB.getInsert(subColumnTable, hasht);
				}
				
			}
			catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		
		private int getCount()
		{
			int i=0,pvs=0;
			ResultSet rs;
			tablization=gin.getId(tablization);
			String count=gin.getId("count");
			String request="select pv from property_details where mid='"+tablization+"' and pid='"+count+"'";
			
			rs=sqlDB.getSelect(request);
			try
			{
				rs.next();
				String pv=rs.getString(1);
				i=pvs=Integer.parseInt(pv);
				pvs++;
				String msg="11 property_details mod $count:"+pvs+" #605";
				//aic.callControlClass(msg);
			}
			catch (Exception e) {
				// TODO: handle exception
			}
			return i;
		}
		
		
		public void searchType(String type,String NoOfProperties,String NoOfObjects)
		{
			
			ResultSet rs;
			String delRequest="",subColumnTable="",request="",table="";		
			Vector<Object> objects=new Vector<Object>();
			Vector<Object> properties=new Vector<Object>();	
			Vector<Object> columnVector=new Vector<Object>();
			Vector<Object> duplicateVector;
			Hashtable<Object,Object> hasht=new Hashtable<Object, Object>();
			try
			{
				String pv=gin.getId(type);
				rs=sqlDB.getSelect("select count(*) from property_details where pid='"+strPid+"' and pv='"+pv+"'");
				rs.next();
				int obj=Integer.parseInt(rs.getString(1));
				
				if(obj>Integer.parseInt(NoOfObjects))
				{
					
					sp=new SearchingPropeties();
						hasht=sp.searchProperty(type);		
					//not do this part --------------------------------------
						cs=new CombinationSearching();
						cs=cs.searchObjects(hasht,NoOfProperties,NoOfObjects);				
					
						for(int i=0;i<cs.objectVector.size();i++)
							objects=(Vector<Object>)cs.objectVector.get(i);
					
						for(int i=0;i<cs.propertyVector.size();i++)
							properties=(Vector<Object>)cs.propertyVector.get(i);
						//-----------------------------------------------------	
						columnVector.add(0,"id");					
						String tableName=getTableName();
					
						//String tableName=type+getMaxId();
						
						for(int i=0;i<properties.size();i++)
						{
							Hashtable<Object,Object> h=(Hashtable<Object, Object>)properties.get(i);
							String tempPid=h.get("pid").toString();			
							String column=gin.getItem(tempPid);				
							columnVector.add(i+1,column.toLowerCase());
						}	
						sqlDB.createTable(tableName, columnVector);
						
						insertInMaster(tableName, columnVector);				
						
						for(int i=0;i<objects.size();i++)
						{
							hasht=new Hashtable<Object, Object>();
							duplicateVector=new Vector<Object>();
							
							String strMid=objects.get(i).toString();			
							String object=gin.getItem(strMid);			
							delRequest="11 property_details del *"+object;	
							
							hasht.put(columnVector.get(0).toString(),strMid);
							
							String query="select mid,pid,pv,vt,ref from property_details where mid='"+strMid+"' order by pid";
							rs=sqlDB.getSelect(query);
							try
							{
								while(rs.next())
								{
									String pid=rs.getString("pid");
									for(int j=0;j<properties.size();j++)
									{
										Hashtable<Object,Object> h=(Hashtable<Object, Object>)properties.get(j);
										String tempPid=h.get("pid").toString();									
										
										if(tempPid.equals(pid))
										{						
											String strPv=rs.getString("pv");					
											if(!strPv.equals("0"))
												strPv=gin.getItem(strPv);
											else
												strPv=rs.getString("vt");								
											
											if(hasht.containsKey(tableName+"."+columnVector.get(j+1).toString()))
											{
												String temp=hasht.get(tableName+"."+columnVector.get(j+1).toString()).toString();
												Hashtable<Object,Object> ht=new Hashtable<Object, Object>();
												ht.put(tableName+"."+columnVector.get(j+1).toString(),temp);
												duplicateVector.add(ht);
											}						
												
											hasht.put(tableName+"."+columnVector.get(j+1).toString(), strPv);
											delRequest=delRequest+"|"+columnVector.get(j+1).toString()+":"+strPv;
											
											String strRef=rs.getString("ref");
											if(!strRef.equals("0"))
											{
												pid=gin.getItem(pid);
												String queryRef="alter table "+tableName+" add column "+pid+"_ref longtext";
												sqlDB.getExecute(queryRef);
												strRef=gin.getItem(strRef);												
												hasht.put(pid+"_ref",strRef);								
											}
											break;
										}					
									}					
								}
							}
							catch (Exception e) {
								// TODO: handle exception
							}	
							
							delRequest=delRequest+"#605";
							System.out.println("Delete Request "+delRequest);
							request="11 property_details add *"+object+"|Table :"+tableName+"#605";	
							
							if(!duplicateVector.isEmpty())
							{
								Vector<Object> tempVector=new Vector<Object>();
								for(int j=0;j<duplicateVector.size();j++)
								{
									Hashtable<Object,Object> temp1=(Hashtable<Object, Object>)duplicateVector.get(j);
									Enumeration<Object> enumt=temp1.keys();
									String str=enumt.nextElement().toString();
									if(hasht.containsKey(str))
									{
										String temp=hasht.get(str).toString();
										temp1=new Hashtable<Object, Object>();
										temp1.put(str,temp);
										duplicateVector.add(temp1);
										tempVector.add(temp1);	
										hasht.remove(str);
									}
									if(!duplicateHashtable.containsKey(str))
									{
										duplicateHashtable.putAll(temp1);
										
											str=str.replace('.','~');
											String []strArr=str.split("~");
											str=strArr[1];									
											Vector<Object> colVector=new Vector<Object>();
											colVector.add("id");
											colVector.add(str);
												
											subColumnTable=str+i;
											sqlDB.createTable(subColumnTable,colVector);
											insertInMaster(subColumnTable, colVector);
									}
								}
								Enumeration<Object> enumt=duplicateHashtable.keys();
								while(enumt.hasMoreElements())
								{
									String subTable=enumt.nextElement().toString();
									for(int k=0;k<tempVector.size();k++)
									{
										Hashtable<Object,Object> temp1=(Hashtable<Object, Object>)tempVector.get(k);
										if(temp1.containsKey(subTable))
										{
											String ref=subTable.replace('.','~');
											String []strArr=ref.split("~");														
											ref=strArr[1];	
											ref=ref+"_ref";
											
											if(hasht.containsKey(ref))
											{
												hasht.remove(ref);
												rs=sqlDB.getSelect("select "+ ref+" from "+tableName);
												try
												{	
													rs.next();
													String strRef=rs.getString(1);												
												}
												catch (Exception e)
												{
													String queryRef="alter table "+tableName+" drop column "+ref;
													sqlDB.getExecute(queryRef);											
												}											
											}
											
											hasht.put(subTable,subColumnTable);
											setInSubTable(strMid,subTable,subColumnTable);
										}
									}
								}				
								sqlDB.getInsert(tableName, hasht);							
							}
							else
								sqlDB.getInsert(tableName, hasht);						
							
							System.out.println("Request is "+request);
								aic.callControlClass(request);	
							System.out.println("Request is "+delRequest);
								//aic.callControlClass(delRequest);
						}
						System.out.println("Table created successfully");
						}
						
					}
					catch (Exception e) 
					{
							// TODO: handle exception
					}					
		}
		
		public String getTable(String pv)
		{
			ResultSet rs;
			String table=" ";
			String strPv=gin.getId(pv);
			String query="select mid from property_details where pid='"+column+"' and pv='"+strPv+"'";
			rs=sqlDB.getSelect(query);
			try
			{
				rs.next();
				table=rs.getString(1);
			}
			catch (Exception e) {
				table=" ";
			}
			return table;
		}
		
		private void insertInMaster(String tableName,Vector<Object> columnVector)
		{
			String request="11 master_table add*"+tableName+"|Type:table |Storage:database";
			
			for(int j=0;j<columnVector.size();j++)
				request=request+"|Column:"+columnVector.get(j).toString();	
			
			request=request+"#605";
			
			System.out.println("Column list is "+request);
			
			aic.callControlClass(request);		
		}
		
		public void setInSubTable(String mid,String subName,String subColumnTable)
		{
			
			ResultSet rs;
		
			Hashtable<Object,Object> hasht=new Hashtable<Object, Object>();
			
			subName=subName.replace('.','~');
			String []strArr=subName.split("~");	
			subName=strArr[1];
			String strPid=gin.getId(subName);	
			
			rs=sqlDB.getSelect("select * from property_details where mid='"+mid+"' and pid='"+strPid+"'");
			try
			{
				while(rs.next())
				{	
					hasht=new Hashtable<Object, Object>();
					hasht.put("id",mid);
					
					String strPv=rs.getString("pv");					
					if(!strPv.equals("0"))
						strPv=gin.getItem(strPv);
					else
						strPv=rs.getString("vt");	
					
					hasht.put(subColumnTable+"."+subName, strPv);
					String strRef=rs.getString("ref");
					if(!strRef.equals("0"))
					{
						String queryRef="alter table "+subColumnTable+" add column "+subName+"_ref longtext";
						sqlDB.getExecute(queryRef);
						strRef=gin.getItem(strRef);								
						hasht.put(subName+"_ref",strRef);								
					}
					sqlDB.getInsert(subColumnTable, hasht);
				}
				
			}
			catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		
		public static void main(String[] args) 
		{
			DBManager daManager=new DBManager();
			daManager.getConnect();
			
			Test td=new Test(daManager);
			td.searchType("item","13","5");
			
		}

	*/}
}
